/*
 * EquilibriumProblem.h
 *
 *  Created on: 27 Jul 2011
 *      Author: Allan
 */

#ifndef EQUILIBRIUMPROBLEM_H_
#define EQUILIBRIUMPROBLEM_H_

// C++ includes
#include <vector>
using namespace std;

// Eigen includes
#include <Eigen/Core>
using namespace Eigen;

// GeoReact includes
#include "EquilibriumCondition.h"
#include "../Multiphase.h"
#include "../MultirootSolver.h"
#include "../CanonicalReactionSystem.h"
#include "../Types.h"

// GeoReact forward declarations
class EquilibriumSolver;

class EquilibriumProblem : public MultirootProblem
{
public:
	EquilibriumProblem();
	
	EquilibriumProblem(const Multiphase& multiphase, const CanonicalReactionSystem& reactions);
	
	virtual ~EquilibriumProblem();
	
	void 
	SetEquilibriumConditions(const vector<EquilibriumCondition>& eConditions);
	
	void 
	SetParameters(double T, double P, const VectorXd& n);
	
	virtual void 
	Function(const VectorXd& njne, VectorXd& F);
	
	virtual void 
	Jacobian(const VectorXd& njne, MatrixXd& J);
	
	void 
	UpdateAuxiliaryData(const VectorXd& njne);
	
	friend class EquilibriumSolver;
	
private:
	/// The multiphase object
	Multiphase multiphase;
	
	/// The system of equilibrium-controlled reactions
	CanonicalReactionSystem reactions;
	
	/// The number of primary (Nj) and equilibrium (Ne) species
	unsigned Nj, Ne;
	
	/// The indexes of the primary (jIndexes) and equilibrium (eIndexes) species
	vector<Index> jIndexes, eIndexes;
	
	/// The stoichiometric matrix of the system of reactions
	MatrixXd vej;
	
	/// The matrices that define the set of equilibrium constraints in the system 
	MatrixXd alpha, beta;
	
	/// The functions that evaluate the right-hand side vector h of the system of equilibrium-constraint equations
	vector<FunctionTPna> hfunctions;
	
	/// The temperature (T) and pressure (P)
	double T, P;
	
	/// The mole vector (n) and activity vector (a) of all species
	VectorXd n, a;
	
	/// The equilibrium constants (Ke) and the reaction quotients (Qe) of the equilibrium-controlled reactions
	VectorXd Ke, Qe;
	
	/// The mole vector of the primary species (nj) and equilibrium species (ne)
	VectorXd nj, ne;
	
	/// The activity vector of the primary species (aj) and equilibrium species (ae)
	VectorXd aj, ae;
	
	/// The right-hand side vector h of the equations for the equilibrium conditions
	VectorXd h;
};

#endif /* EQUILIBRIUMPROBLEM_H_ */
